home *** CD-ROM | disk | FTP | other *** search
- Path: news.gate.net!pslfl2-40
- From: bhutto@gate.net (William Hutto)
- Newsgroups: comp.lang.c
- Subject: Re: cpp question
- Date: 23 Jan 1996 10:02:12 GMT
- Organization: CyberGate, Inc.
- Message-ID: <4e2bn4$2d8c@news.gate.net>
- References: <4drm99$j0m@peabody.colorado.edu> <4dtf1f$21r4@news.gate.net> <4e0580$9v5@solutions.solon.com>
- NNTP-Posting-Host: pslfl2-40.gate.net
- X-Newsreader: News Xpress Version 1.0 Beta #4
-
- In article <4e0580$9v5@solutions.solon.com>,
- seebs@solutions.solon.com (Peter Seebach) spake:
- ;In article <4dtf1f$21r4@news.gate.net>, William Hutto <bhutto@gate.net>
- wrote:
- ;>In article <4drm99$j0m@peabody.colorado.edu>,
- ;> woodjr@rintintin.Colorado.EDU (WOOD JAMEY RYAN) spake:
- ;>;I have something like this:
- ;>; #define USERLEN 8
- ;>; #define HOSTLEN 15
- ;
- ;>; char name[] = "woodjr";
- ;>; char host[] = "really.long.hostname.com";
- ;>; char s[100];
- ;
- ;>; sprintf(s, "%.USERLENs@%.HOSTLENs", name, host);
- ;
- ;>;And I want cpp to parse the sprintf line to:
- ;
- ;>; sprintf(s, "%.8s@%.15s", name, host);
- ;
- ;>;Which it (of course) isn't doing. Is there some syntax I
- ;>;can use to get it to do this?
- ;
- ;>There shur is:
- ;
- ;> sprintf(s, "%.*s@%.*s", USERLEN, name, HOSTLEN, host);
- ;
- ;>The asterisk in the width.precision fields in the formatting sequence allows
- ;>you to include the size as an integer value in your parameter list.
- ;
- ;Yes, but that doesn't do what he wants. How about
- ;#define STR(x) RSTR(x)
- ;#define RSTR(x) #x
- ;
- ; sprintf(s, "%." STR(USERLEN) "s@%." STR(HOSTLEN) "s", name , host);
- ;
-
- Well, I don't see any difference in the outcome. I didn't think he
- wanted to actually paste the numbers into the string. However, try your
- solution with variables:
-
- #define STR(x) RSTR(x)
- #define RSTR(x) #x
-
- char *name = "bhutto", *host = "gate.net";
- unsigned userlen = 8, hostlen = 15;
-
- sprintf(s, "%." STR(userlen) "s@%." STR(hostlen) "s", name , host);
-
- which yields:
-
- sprintf(s, "%.userlens@%.hostlens" , name, host);
- %.u %.ho
- ^^^ ^^^^
- So if you were to do this you should, of course, cast the pointers to unsigned
- int. :)
-
- Bill
-
-
-
- William Hutto
- bhutto@gate.net
- N4YMN
-
- "Whatcha got on?...Your mind?"
-